package com.hhkj.api.order.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.order.model.dopo.OrderItemPo;
import com.hhkj.admin.order.service.base.OrderItemService;
import com.hhkj.admin.student.common.cont.StudentConst;
import com.hhkj.admin.student.domain.Student;
import com.hhkj.admin.student.domain.StudentCourse;
import com.hhkj.admin.student.model.dopo.StudentCoursePo;
import com.hhkj.admin.student.service.extend.StudentCourseExtendService;
import com.hhkj.admin.student.service.extend.StudentExtendService;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.model.dopo.TrainDo;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.api.order.model.req.ApiOrderItemCheckReq;
import com.hhkj.api.order.model.req.ApiOrderItemReq;
import com.hhkj.api.order.model.res.ApiOrderItemRes;
import com.hhkj.api.order.service.biz.ApiOrderItemBizService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.GlobalCheckException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApiOrderItemBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/10/30 11:51
 */
@Service
public class ApiOrderItemBizServiceImpl implements ApiOrderItemBizService {

    @Autowired
    private StudentCourseExtendService studentCourseExtendService;
    @Autowired
    private StudentExtendService studentExtendService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private TrainService trainService;

    @Override
    public List<Train> listStudentTrain(ApiOrderItemReq apiOrderItemReq) {
        Long studentCourseId = apiOrderItemReq.getStudentCourseId();
        StudentCourse studentCourse = studentCourseExtendService.getByIdNotNull(studentCourseId);
        TrainDo trainDo = new TrainDo();
        trainDo.setStudentId(studentCourse.getStudentId());
        trainDo.setCourseId(studentCourse.getCourseId());
        trainDo.setCreateTimeStart(studentCourse.getStartDate());
        trainDo.setCreateTimeEnd(studentCourse.getLineDate());
        return trainService.listStudentTrainListPoByDo(trainDo);
    }

    @Override
    public List<ApiOrderItemRes> list(ApiOrderItemReq req) {
        List<OrderItemPo> orderItemPoList = orderItemService.listPoByDo(req);
        orderItemPoList = orderItemService.fillLoginFlag(orderItemPoList);
        return BeanUtil.copyToList(orderItemPoList, ApiOrderItemRes.class);
    }

    @Override
    public void check(ApiOrderItemCheckReq itemReq) {
        //1、同一个身份证、科目（账号已经生效，不支持录入）
        StudentCoursePo studentCoursePo
                = studentCourseExtendService.getPo(
                itemReq.getIdCard(),
                itemReq.getCourseId(),
                StudentConst.AuditCode.STATUS_PASS.getCode());
        if (ObjectUtil.isNotNull(studentCoursePo)) {
            throw new GlobalCheckException(400, "该学员已在系统中报名，且在有效期内");
        }
        //2、同一个身份证、姓名不同（提示 该身份证人员在系统中的名字，让用户选择修改录入信息还是修改系统中型姓名）
        Student studentPo = studentExtendService.getByIdCard(itemReq.getIdCard());
        if (ObjectUtil.isNotNull(studentPo) && !studentPo.getName().equals(itemReq.getName())) {
            throw new GlobalCheckException(HttpStatus.WARN, "已存在身份证号相同学员，系统内姓名为：\n"+ studentPo.getName() + "\n若有误请联系客服修改");
        }
    }
}
